package com.itec.learn.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2017/1/12 0012.
 */
public class Client {

    public static void main(String[] args) throws IOException {
        OutputStream out = null;
        PrintWriter pw = null;
        String input = null;
        while (!"bye".equals(input)){
            try {
                Socket socket = new Socket("127.0.0.1",9999);
                System.out.println("Connect to server....");
                out = socket.getOutputStream();
                pw = new PrintWriter(out);
                input = readFromConsole("Please input string：");
                pw.write(input);
                pw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (pw != null){
                    pw.close();
                }
                if (out != null){
                    out.close();
                }
            }
        }
    }

    public static String readFromConsole(String prompt){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        try {
            System.out.print(prompt);
            str = br.readLine();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

}
